package com.lancoo.znbkxx.teacher.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.PopupWindow
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.adapter.ClassListAdapter
import com.lancoo.znbkxx.teacher.adapter.SpacesItemDecoration
import com.lancoo.znbkxx.teacher.model.data.BaseInfo
import com.lancoo.znbkxx.teacher.utils.DensityUtils

/**
 * @author : Admin
 * @date   : 2019/10/23 15:08
 * @desc   :
 */
class ClassMenu(context: Context, data: MutableList<String>, listener: BaseQuickAdapter.OnItemClickListener) : PopupWindow() {
    var adapter: ClassListAdapter

    init {
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val mPopView = inflater.inflate(R.layout.menu_class, null)
        val rv = mPopView.findViewById<RecyclerView>(R.id.rv)
        adapter = ClassListAdapter(R.layout.item_spinner, data)
        adapter.onItemClickListener = listener
        val map = hashMapOf<String, Int>()
        map[SpacesItemDecoration.TOP_SPACE] = DensityUtils.dp2px(context, 5f)
        map[SpacesItemDecoration.BOTTOM_SPACE] = DensityUtils.dp2px(context, 5f)
        val itemDecoration = SpacesItemDecoration(map)
        rv.addItemDecoration(itemDecoration)
        rv.layoutManager =
                LinearLayoutManager(context).apply { orientation = RecyclerView.VERTICAL }
        rv.adapter = adapter
        this.contentView = mPopView
        this.width = WindowManager.LayoutParams.WRAP_CONTENT
        this.height = WindowManager.LayoutParams.WRAP_CONTENT
        this.isOutsideTouchable = true
        this.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        this.isFocusable = true
        update()
    }
}